<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 5/26/21
 * Time: 5:18 PM
 */

namespace app\api\controller\v1;

use app\api\model\TeamUser as TeamUserModel;
use app\api\model\User as UserModel;
use LinCmsTp5\exception\ParameterException;

// 团员信息
class TeamUser
{
    /**
     * 获取列表
     * @validate('PagingParameter')
     */
    public function getDataList($name,$type,$team_id,$page,$limit)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($team_id) {
            $where[] = ['team_id', 'eq', $team_id];
        }
        if($type) {
            $where[] = ['type','eq',$type];
        }
        $query = TeamUserModel::where($where)->with('team,user')->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['type'] == 1) {
                $item['type_name'] = '团长';
            } elseif($item['type'] == 2) {
                $item['type_name'] = '团员';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取团长详情
     *  @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return TeamUserModel::get($id);
    }


    /**
     * 创建信息
     * @validate("CheckTeamOwner")
     */
    public function createData($name,$phone,$remarks,$desc,$team_id,$type,$user_id)
    {
        TeamUserModel::create([
            'name' => $name,
            'phone' => $phone,
            'remarks' => $remarks,
            'desc' => $desc,
            'team_id' => $team_id,
            'type' => $type,
            'user_id' => $user_id
        ]);
        return writeJson(201, [], '添加成功！');
    }

    /**
     * 修改信息
     * @validate("CheckTeamOwner")
     */
    public function updateData($id,$name,$phone,$remarks,$desc,$team_id,$type,$user_id)
    {
        $info = TeamUserModel::where('id',$id)->find();
        $user = UserModel::where('id',$user_id)->find();
        if($info['user_id'] != $user['id']) {
            if ($user['type'] != 1) {
                throw new ParameterException([
                    'msg' => [
                        'team_owner' => '该用户已经绑定角色'
                    ]
                ]);
            }
        }

        if($info) {
            $info->name = $name;
            $info->phone = $phone;
            $info->remarks = $remarks;
            $info->desc = $desc;
            $info->team_id = $team_id;
            $info->type = $type;
            $info->user_id = $user_id;
            $info->save();
            if($type == 1) {
                $user_type = 5;
            }elseif($type == 2) {
                $user_type = 6;
            }
            UserModel::where('id',$user_id)->update([
                'type'=> $user_type
            ]);
        }
        return writeJson(202, [], '修改成功！');
    }


    /**
     * 删除信息
     * @validate('IdMustBePositiveInt')
     */
    public function deleteData($id)
    {
        $info = TeamUserModel::where('id',$id)->find();
        if($info) {
            $user_id = $info['user_id'];
            TeamUserModel::destroy($id);
            UserModel::where('id',$user_id)->update([
                'type' => 1,
                'team_id' => null
            ]);
        }
        return writeJson(203, [], '删除成功！');
    }
}